<?php
namespace app\admin\controller;
use think\Controller;
use think\Request;
use think\Config;
use think\Db;
use PHPExcel_IOFactory;
use PHPExcel;

class Member extends Controller
{
    public function index()
    {
        return $this->fetch();
    }
    /**
     * 成员列表
     * @Author   YGF
     * @DateTime 2018-06-27T15:24:18+0800
     */
    public function listXY(Request $request)
    {
        $page   = $request->get('page');
        $limit  = $request->get('limit');
        $start  = ($page - 1) * $limit;
        $condition = $request->get('condition');
        $map['b.post']           = array('eq', 0);


        if($condition){
            $map['a.truename']   = array('like', '%'.$condition.'%');
        }

        $data=Db::name('user a')->join('web_job b', 'a.uid=b.uid')->where($map)->limit($start, $limit)->select();
        $count=Db::name('user a')->join('web_job b', 'a.uid=b.uid')->where($map)->count();
        return getTableJson(0,'success',$count,$data);     
    }

    /**
     * 查看成员
     * @Author   YGF
     * @DateTime 2018-06-27T15:23:18+0800
     */
    public function showMember(Request $request){
            $id=input('id');

            $data=Db::name('user')->where('uid',$id)->find();
            $this->assign('data',$data);
        return $this->fetch();
    }

    /**
     * 新增成员
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function addMember(Request $request)
    {
    	if($request->isPost()){
      		$data=$request->post();
            unset($data['file']);
            $map['b.post']      = array('eq', 0);
            $map['a.username']  = $data['username'];
            //判断是已存在该成员
            $user       =Db::name('user a')->join('web_job b', 'a.uid=b.uid')
                        ->where($map)
                        ->find();
            if($user){
                return getJson(1,'已存在该学员');
            }
            $data['true_id']    = $data['password'];
            $data['password']   = md5($data['password']);


            //不存在则添加
	          $res1=Db::name('user')->insertGetId($data);
              $res2=Db::name('job')->insertGetId(array('uid'=>$res1, 'post' => 0));
	          if($res1 && $res2){
	          	return getJson(0,'新增成功');
	          }
      			return getJson(1,'新增失败');
      	}
        return $this->fetch();
    }

    /**
     * 添加时检查是否有该成员学号
     * @Author   YGF
     * @DateTime 2018-06-29T15:21:18+0800
     */
    public function findMember(Request $request)
    {
        if($request->isPost()){
           $username=input('id');
           $res=Db::name('user')
                   ->where('username',$username)
                   ->find();
              if($res){
                return getJson(1);
              }
                return getJson(0);
        }
        return $this->fetch();
    }


    /**
     * 修改成员信息
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function editMember(Request $request)
    {
        
 		$id=input('id');
 		$data=Db::name('user a')->join('web_job b', 'a.uid=b.uid')
                ->where('a.uid',$id)
                ->find();
 		$this->assign('data',$data);

    	if($request->isPost()){
            $data=$request->post();
            unset($data['file']);
              $res=Db::name('user')
                      ->where('uid',$id)
                      ->update($data);
              if($res){
                return getJson(0,'修改成功');
              }
                return getJson(1,'数据没变更，无需提交');
        }
       return $this->fetch();
    }
     

    /**
     * 删除成员
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function delMember()
    {
        $id=input('id');
        $res=Db::name('user')->where('uid',$id)->delete();
        // $resWorks=Db::name('works')->where('id',$id)->delete();
        if($res){
            return getJson(0,'删除成功');
        }
        return getJson(1,'删除失败');
    }

      /**
     * 批量删除
     * @Author   YGF
     * @DateTime 2018-06-27T17:23:18+0800
     */
    public function batchDel (Request $request){
       $ids = $request->post('ids/a');
        $table = Config::get("database.prefix") . 'member';
        $tabWorks = Config::get("database.prefix") . 'works';
        $res = Db::table($table)->delete($ids);
        $works = Db::table($table)->delete($ids);
        if($res){
            return getJson(0,'删除成功');
        }
        return getJson(1,'删除失败');
    }


   /**
     * 成员作品区域
     * @Author   YGF
     * @DateTime 2018-06-27T14:25:18+0800-----------------------
     */
    
    public  function works(){

       return $this->fetch();
    }


    /**
     * 查看作品
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function showWorks(){
            $id=input('id');
            $data=Db::name('user')->where('uid',$id)->find();
            $this->assign('data',$data);
        return $this->fetch();
    }



   /**
     * 作品列表
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function listWX(Request $request){
        $JOB                = ['学生', '电器类', '木工', '电子信息类', '玻璃维修'];
        $page           = $request->get('page');
        $limit          = $request->get('limit');
        $start = ($page - 1) * $limit;
        $condition = $request->get('condition');
        $map['b.post']           = array(array('neq', 0), array('neq', 999));

        if($condition){
            $map['a.truename']   = array('LIKE', '%'.$condition.'%');
        }



        $data=Db::name('user a')->join('web_job b', 'a.uid=b.uid')->where($map)->limit($start, $limit)->select();
        $count=Db::name('user a')->join('web_job b', 'a.uid=b.uid')->where($map)->count();

        foreach ($data as $key => &$value) {
            $value['post_name']   = $JOB[$value['post']];
        }

        return getTableJson(0,'success',$count,$data); 
    
    }

    /**
     * 添加作品
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function addWorks(Request $request){

        if($request->isPost()) {
            $data=$request->post();
            $map['b.post']      = array(array('neq', 0), array('neq', 999));
            $map['a.username']  = $data['username'];
            $post               = $data['post'];
            
            unset($data['file']);
            unset($data['post']);
            //判断是已存在该成员


            $user       =Db::name('user a')->join('web_job b', 'a.uid=b.uid')
                        ->where($map)
                        ->find();
            if($user){
                return getJson(1,'已存在该学员');
            }
            $data['true_id']    = $data['password'];
            $data['password']   = md5($data['password']);
    
            //不存在则添加
              $res1=Db::name('user')->insertGetId($data);
              $res2=Db::name('job')->insertGetId(array('uid'=>$res1, 'post' => $post));
              if($res1 && $res2){
                return getJson(0,'新增成功');
              }
                return getJson(1,'新增失败');
        }
        return $this->fetch();
    }


    /**
     * 修改作品
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function editWorks(Request $request){

        $id=input('id');
        $data=Db::name('user a')->join('web_job b', 'a.uid=b.uid')->where('a.uid',$id)->find();
        $this->assign('data',$data);
        if($request->isPost()){
                    $data=$request->post();
                    $post=$data['post'];
                    unset($data['post']);
                    unset($data['file']);
                      $res1=Db::name('user')->where('uid',$id)->update($data);
                      $req2=Db::name('job')->where('uid', $id)->update(array('post' => $post));
                      if($res1 && $res2){
                        return getJson(0,'修改成功');
                      }
                        return getJson(1,'数据没变更，无需提交');
                }
                return $this->fetch();
    }
    /**
     * 删除作品
     * @Author   YGF
     * @DateTime 2018-06-27T15:21:18+0800
     */
    public function delWorks(){
         $id=input('id');
        $res=Db::name('user')->where('uid',$id)->delete();
        if($res){
            return getJson(0,'删除成功');
        }
        return getJson(1,'删除失败');

    }

      /**
     * 批量删除
     * @Author   YGF
     * @DateTime 2018-06-27T17:23:18+0800
     */
    public function batchDelWorks (Request $request){
       $ids = $request->post('ids/a');
        $table = Config::get("database.prefix") . 'works';
        $res = Db::table($table)
                ->delete($ids);

        if($res){
            return getJson(0,'删除成功');
        }
        return getJson(1,'删除失败');
    }

    public function excel_upload(Request $request) {
        if($request->isPost()) {
            $PHPExcel       = new \PHPExcel();
            $file           = $this->request->file('excel');
        
            if(!empty($file)){

                $info           = $file->move(ROOT_PATH . 'public/uploads' . DS . 'Excel');
                if($info) {
                    $exclePath  = $info->getSaveName();  //获取文件名  
                    $file_name  = ROOT_PATH . 'ROOT_PATH/uploads' . DS . 'Excel' . DS . $exclePath; 
                }

                if(@fopen($file_name, 'r')){
                     $objReader = PHPExcel_IOFactory::createReader('Excel2007');    

                    if(!$objReader->canRead($file_name)) {
                        $objReader = PHPExcel_IOFactory::createReader('Excel5');
                        if( ! $objReader->canRead($file_name)) {
                          die('仅支持 .xls 类型的文件 ！');
                        }
                    } 

                    $objReader->setReadDataOnly(true);

                    $objPHPExcel    = $objReader->load($file_name);
                
                    $objWorksheet   = $objPHPExcel->getActiveSheet();
                
                    $highestRow     = $objWorksheet->getHighestRow();
                
                    $highestColumn  = $objWorksheet->getHighestColumn(); 
                    
                    $excel_array=$objPHPExcel->getSheet(0)->toArray();

                    $arr            = ['truename', 'username', ];
                    $err            = [];
                    $i=0;  


                    

                    foreach($excel_array as $k=>$v) {  
                        $user           = $this->student_excel($v);

                        if(is_array($user)) {
                            $id         = Db::name('user')->insertGetId($user);
                            $res        = Db::name('job')->insertGetId(array('uid' => $id, 'post' => 0));
                        } else {
                            $err[]      = $user;
                        }
                        $i++;
                    }

                    if(!empty($err)) {
                        // 成功一部分吧
                        $this->success('录取一部分学生,以下学生没有录取到' . implode(',', $err));
                    } else {
                        $this->success('成功录入所有信息');
                    }

                }
            } else {
                $this->error('文件不存在');
            }
        }
    }

    // 学生
    public function student_excel($arr) {
        $map['b.post']      = 0;
        $map['a.username']  = $arr[1];
        $user               = Db::name('user a')->join('web_job b', 'a.uid=b.uid')->where($map)->find();

        if($user) {
            return $arr[1];
        }

        return array(
            'truename'      => $arr[0],
            'nickname'      => $arr[0],
            'username'      => $arr[1],
            'password'      => md5($arr[2]),
            'true_id'       => $arr[2],
            'sex'           => $arr[3] == '男' ? 0 : 1,
            'address'       => $arr[4],
            'mobile'        => $arr[5],
            'email'         => $arr[6],
            'class'         => $arr[7],
            'status'        => '1',
            'reg_time'      => time()
        );
    }

}